//
//  CALMeasurementViewController.m
//  CalCounter
//
//  Created by Le thanh Lam on 06/09/2013.
//  Copyright (c) 2013 Le thanh Lam. All rights reserved.
//

#import "CALMeasurementViewController.h"
#import "DateTimeView.h"
#import "DataHelper.h"
#import "NSDate+Ex.h"
#import "NSString+Ex.h"

@interface CALMeasurementViewController ()<DateTimeViewDelegate>{
    __weak IBOutlet DateTimeView *dateBarView;
}

@end

@implementation CALMeasurementViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [dateBarView refresh];
}

- (void)viewDidLoad
{
    [self.navigationItem setTitle:NSLocalizedString(@"measurement", nil)];
    //add date time view

    //text field
    txtHipSize.delegate = self;
    txtWaistSize.delegate = self;
    txtNeckCircum.delegate = self;
    //scrview
    [scrView setContentSize:CGSizeMake(320, 500)];
    //add save button on navigation
    UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(btnSaveClicked:)];
    self.navigationItem.rightBarButtonItem = btnSave;

    CALDiaryInfo *info = [[DataHelper sharedDatabase] getDiaryInfoByDate:[DateTimeView selectDate]];
    
    txtHipSize.text = info.diaryHip == 0 ? nil : [NSString stringWithFormat:@"%1.1f",info.diaryHip];
    txtWaistSize.text = info.diaryWaist == 0 ? nil : [NSString stringWithFormat:@"%1.1f",info.diaryWaist];
    txtNeckCircum.text = info.diaryBreast == 0 ? nil : [NSString stringWithFormat:@"%1.1f",info.diaryBreast];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)btnSaveClicked:(id)sender{
    txtHipSize.text = [txtHipSize.text stringByReplacingOccurrencesOfString:@"," withString:@"."];
    txtNeckCircum.text = [txtNeckCircum.text stringByReplacingOccurrencesOfString:@"," withString:@"."];
    txtWaistSize.text = [txtWaistSize.text stringByReplacingOccurrencesOfString:@"," withString:@"."];
    static NSString *floatRegex = @"^(?:[1-9]\\d*|0)?(?:\\.\\d+)?$";
    double hipVal = 0;
    double neckVal = 0;
    double waistVal = 0;
    
    if([txtHipSize.text length] != 0){
        if(![txtHipSize.text isMatchRegex:floatRegex]){
            [[[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"InvalidValue", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
            return;
        }
        hipVal = [txtHipSize.text doubleValue];
    }
    if([txtNeckCircum.text length] != 0){
        if(![txtNeckCircum.text isMatchRegex:floatRegex]){
            [[[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"InvalidValue", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
            return;
        }
        neckVal = [txtNeckCircum.text doubleValue];
    }
    if([txtWaistSize.text length] != 0){
        if(![txtWaistSize.text isMatchRegex:floatRegex]){
            [[[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"InvalidValue", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
            return;
        }
        waistVal = [txtWaistSize.text doubleValue];
    }
    
    if(hipVal > 9999 ||
       waistVal > 9999 ||
       neckVal > 9999){
        [[[UIAlertView alloc] initWithTitle:@"" message:NSLocalizedString(@"BigValue", nil) delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
        return;
    }
    
    BOOL result = [[DataHelper sharedDatabase] updateHipSize:hipVal
                                     waistSize:waistVal
                                      neckSize:neckVal
                                        inDate:[[DateTimeView selectDate] dateString]];
    if(result){
        [self.navigationController popViewControllerAnimated:YES];
    }
}


- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if(textField == txtWaistSize){
        [scrView setContentOffset:CGPointMake(0, 60) animated:YES];
    }else if(textField == txtNeckCircum){
        [scrView setContentOffset:CGPointMake(0, 120) animated:YES];
    }else{
        [scrView setContentOffset:CGPointMake(0, 20) animated:YES];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    [scrView setContentOffset:CGPointMake(0, 0) animated:YES];
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DateTimeViewDelegate
- (void)whenChangeTime:(NSDate *)_date{
    CALDiaryInfo *info = [[DataHelper sharedDatabase] getDiaryInfoByDate:[DateTimeView selectDate]];
    txtHipSize.text = info.diaryHip == 0 ? nil : [NSString stringWithFormat:@"%1.1f",info.diaryHip];
    txtWaistSize.text = info.diaryWaist == 0 ? nil : [NSString stringWithFormat:@"%1.1f",info.diaryWaist];
    txtNeckCircum.text = info.diaryBreast == 0 ? nil : [NSString stringWithFormat:@"%1.1f",info.diaryBreast];
}
#pragma mark -
@end
